#include <stdio.h>
#include <string.h>
#include <conio.h>
#include "InputFormat.h"

/**
 * 格式化输入
 *
 * inputString: 存放输入的字符串
 * start: 输入的最小数字
 * end: 输入的最大数字
 * maxLength: 输入的最大位数
 * inputType: 输入字母或数字的参数 1为字母，2为数字
 */
int FormatInputString(char inputString[], char start, char end, int maxLength, int inputType)
{
    int result = 0;
    switch (inputType)
    {
    case 1:
        result = ControlMaxInput(inputString, maxLength);
    case 2:
        result = ControlInputLength(inputString, start, end, maxLength);
    default:
        break;
    }
    return result;
}

// 输入字母格式化
// inputString: 存放输入的字符串
// maxLength: 输入的最大长度
int ControlMaxInput(char inputString[], int maxLength)
{
    char inputChar;
    char result = 0;
    int length = 0;
    if (maxLength <= 10)
    {
        inputChar = getch();
        while (length == 0 || inputChar != 13) // 判断输入字符的合法性
        {
            if (length > 0 && inputChar == 8)
            {
                printf("\b \b");
                length--;
            }
            else if (((inputChar >= 'a' && inputChar <= 'z') || inputChar >= 'A' && inputChar <= 'Z') && length < maxLength)
            {
                if (maxLength == 1)
                {
                    if (inputChar == 'y' || inputChar == 'n')
                    {
                        result = inputChar;
                        printf("%c", result);
                        length++;
                    }
                }
                else
                {
                    inputString[length] = inputChar;
                    printf("%c", inputString[length]);
                    length++;
                }
            }
            inputChar = getch();
        }

        if (maxLength != 1)
        {
            inputString[length] = '\0';
        }
    }
    else
    {
        fflush(stdin);
        gets(inputString);
    }
    return (result);
}

// 输入数字格式化
// inputString: 存放输入的字符串
// start: 输入的最小数字
// end: 输入的最大数字
// maxLength: 输入的最大位数
int ControlInputLength(char inputString[], char start, char end, int maxLength)
{
    char inputChar;
    int i = 0;
    int result = 0;
    int stringLength;
    if (inputString != NULL && inputString[0] != 0)
    {
        stringLength = strlen(inputString);
        for (; inputString[i] != '\0'; i++)
        {
            result = result * 10 + inputString[i] - 48; // 字符转化成数字
        }
        result = result + 1; // 数字加一
        for (i = stringLength - 1; i >= 0; i--)
        {
            inputString[i] = (result % 10 + 48); // 数字再转换为字符
            result = result / 10;
        }
        inputString[stringLength] = '\0';
        return 0;
    }

    inputChar = getch();
    while (i == 0 || inputChar != 13)
    {
        if (inputChar == 27 && inputString != NULL)
        {
            strcpy(inputString, "0");
            return 0;
        }
        if (1 > 0 && inputChar == 8)
        {
            printf("\b \b");
            i--;
        }
        else if ((inputChar >= start && inputChar <= end) && i < maxLength)
        {
            // 输入范围的合法性判断
            if (maxLength == 6) // 密码输入，暗文显示
            {
                inputString[i] = inputChar;
                printf("%c", '*');
                i++;
            }
            else if (maxLength == 1)
            {
                result = inputChar - 48;
                printf("%d", result);
                i++;
            }
            else
            {
                inputString[i] = inputChar;
                printf("%c", inputChar);
                i++;
            }
        }
        inputChar = getch();
    }

    if (maxLength == 1)
    {
        return result; // 返回一个数字，用来功能选择
    }
    else
    {
        inputString[i] = '\0';
        for (i = 0; inputString[i] != '\0'; i++)
        {
            result = result * 10 + inputString[i] - 48; // 字符转化成int型
        }
        return result; // 返回int型的数据，编号输入
    }
}